<?php
require_once 'function.inc.php';
require_once 'HttpRequst.php';
require_once 'AopSdk.php';


class Alipay_con extends Lxbase{

    public function index(){

        header ( "Content-type: text/html; charset=utf-8" );

        //創建菜单
        //https://fuwu.alipay.com/platform/doc.htm#c0602
        if(HttpRequest::getRequest('param') == 'menuadd')
        {
            $biz_content = array(
                'button' => array(
                    array("actionParam"=> "ZFB_HFCZ",
                        "actionType"=> "out",
                        "name"=> "话费充值",
                        'id' => 17,
                        'pid' => 0,
                        'subButton' => Array()),
                    array(
                        "name"=> "查询",
                        "subButton" => array(
                            /*array("actionParam"=> "ZFB_YECX",
                                "actionType"=> "out",
                                "name"=> "余额查询"),
                            array("actionParam"=> "95188",
                                "actionType"=> "tel",
                                "name"=> "联系我们"),*/
                            array("actionParam"=> "http://www.baidu.com",
                                "actionType"=> "link",
                                "name"=> "话费查询"),
                        ),

                    ),
                    array("actionParam"=> "http=>//example.com/b.php",
                        "actionType"=> "link",
                        "name"=> "帐号绑定"),

                ),
            );

            $biz_content = json_encode($biz_content);

//            include 'AlipayMobilePublicMenuAddRequest.php';
            $request = new AlipayMobilePublicMenuAddRequest();
//            echo "<pre>";
//            var_dump($request);
            $request->setBizContent($biz_content);

            $result = aopclient_request_execute($request);

//            print_r($result);
            if($result != null && $result->alipay_mobile_public_menu_add_response->code == 200)
            {
                echo $result->alipay_mobile_public_menu_add_response->code;
                echo $result->alipay_mobile_public_menu_add_response->msg;

            }
        }

        //更新菜单
        //https://fuwu.alipay.com/platform/doc.htm#c0603
        if(HttpRequest::getRequest('param') == 'menuupdate')
        {
            $biz_content = array(
                'button' => array(
                    array("actionParam"=> "ZFB_WDZH",
                        "actionType"=> "out",
                        "name"=> "我的账户通"),
                    array(
                        "name"=> "查询",
                        "subButton" => array(
                            array("actionParam"=> "ZFB_YECX",
                                "actionType"=> "out",
                                "name"=> "余额查询"),
                            array("actionParam"=> "ZFB_LLCX",
                                "actionType"=> "link",
                                "name"=> "流量查询"),
                        ),

                    ),
                    array("actionParam"=> "http://m.alipay.com",
                        "actionType"=> "link",
                        "name"=> "最新优惠"),

                ),
            );

            $biz_content = json_encode($biz_content);

            $request = new AlipayMobilePublicMenuUpdateRequest();
            $request->setBizContent($biz_content);

            $result = aopclient_request_execute($request);

//            echo "<pre>";
//            print_r($result);
            if($result != null && $result->alipay_mobile_public_menu_update_response->code == 200)
            {
                echo $result->alipay_mobile_public_menu_update_response->code;
                echo $result->alipay_mobile_public_menu_update_response->msg;

            }
        }

        //查询菜单
        //https://fuwu.alipay.com/platform/doc.htm#c0601
        if(HttpRequest::getRequest('param') == 'menuget')
        {
            $request = new AlipayMobilePublicMenuGetRequest();
//            $request->getApiMethodName();

            $result = aopclient_request_execute($request);

//            echo "<pre>";
//            print_r($result);
            if($result != null && $result->alipay_mobile_public_menu_get_response->code == 200)
            {
                $res =  $result->alipay_mobile_public_menu_get_response->menu_content;
//                print_r(json_decode($res));
                print_r(json_decode($res, true));

            }
        }

        //生成带参数的二维码
        //http://doc.open.alipay.com/doc2/apiDetail.htm?spm=0.0.0.0.eEDAmK&docType=4&apiId=234
        if(HttpRequest::getRequest('param') == 'qrcode')
        {
            $biz_content = '{
                "codeInfo": {
                    "scene": {
                        "sceneId": "1234"
                    }
                },
                "codeType": "TEMP",
                "expireSecond": "1800",
                "showLogo": "N"
            }';

            $request = new AlipayMobilePublicQrcodeCreateRequest();
            $request->setBizContent($biz_content);

            $result = aopclient_request_execute($request);

            echo "<pre>";
            print_r($result);

            if($result != null && $result->alipay_mobile_public_qrcode_create_response->code == 200)
            {
                $img_url = $result->alipay_mobile_public_qrcode_create_response->code_img;
                echo "<img src='$img_url'/>";

            }
        }

        //群发消息
        //https://fuwu.alipay.com/platform/doc.htm#c0402
        if(HttpRequest::getRequest('param') == 'totalsend')
        {
            $biz_content = '{
                "msgType": "text",
                "text": {
                    "content": "hello"
                }
            }';

            $request = new AlipayMobilePublicMessageTotalSendRequest();
            $request->setBizContent($biz_content);

            $result = aopclient_request_execute($request);

            echo "<pre>";
            print_r($result);

            if($result != null && $result->alipay_mobile_public_message_total_send_response->code == 200)
            {
                echo $result->alipay_mobile_public_message_total_send_response->code;


            }
        }

        //添加标签
        //https://fuwu.alipay.com/platform/doc.htm#c13_label_crud
        if(HttpRequest::getRequest('param') == 'labeladd')
        {
            $biz_content = '{"name" : "label add"}';

            $request = new AlipayMobilePublicLabelAddRequest();
            $request->setBizContent($biz_content);

            $result = aopclient_request_execute($request);

            echo "<pre>";
            print_r($result);
            if($result != null && $result->alipay_mobile_public_label_add_response->code == 200)
            {
                echo $result->alipay_mobile_public_label_add_response->name;

            }
        }

        //查询标签
        //https://fuwu.alipay.com/platform/doc.htm#c13_label_crud
        if(HttpRequest::getRequest('param') == 'labelquery')
        {

            $request = new AlipayMobilePublicLabelQueryRequest();

            $result = aopclient_request_execute($request);

            echo "<pre>";
            print_r($result);
            if($result != null && $result->alipay_mobile_public_label_query_response->code == 200)
            {
                echo $result->alipay_mobile_public_label_query_response->labels;

            }
        }

        //修改标签
        //https://fuwu.alipay.com/platform/doc.htm#c13_label_crud
        if(HttpRequest::getRequest('param') == 'labelupdate')
        {
            $biz_content = '{"id" : "100711","name" : "update label"}';

            $request = new AlipayMobilePublicLabelUpdateRequest();
            $request->setBizContent($biz_content);

            $result = aopclient_request_execute($request);

            echo "<pre>";
            print_r($result);
            if($result != null && $result->alipay_mobile_public_label_update_response->code == 200)
            {
                echo $result->alipay_mobile_public_label_update_response->id;

            }
        }

        //删除标签
        //https://fuwu.alipay.com/platform/doc.htm#c13_label_crud
        if(HttpRequest::getRequest('param') == 'labeldelete ')
        {
            $biz_content = '{"id" : "100711"}';

            $request = new AlipayMobilePublicLabelDeleteRequest();
            $request->setBizContent($biz_content);

            $result = aopclient_request_execute($request);

            echo "<pre>";
            print_r($result);
            if($result != null && $result->alipay_mobile_public_label_delete_response->code == 200)
            {
                echo $result->alipay_mobile_public_label_delete_response->id;

            }
        }

        //获取用户auth_code
        $auth_code = $_GET['auth_code'];
//        var_dump($auth_code);

        //使用auth_code换取接口访问令牌及用户OpenId
        if(HttpRequest::getRequest('param') == 'getopenid')
        {
            $request = new AlipaySystemOauthTokenRequest();
            $request->setCode($auth_code);
            $request->setGrantType("authorization_code");

            $result = aopclient_request_execute($request);

            echo "<pre>";
            print_r($result);
            if($result != null )
            {
                $auth_token = $result->alipay_system_oauth_token_response->access_token;
                //echo $result->alipay_system_oauth_token_response->alipay_user_id; //当前用户的OpenId
                //echo $result->alipay_system_oauth_token_response->user_id;//对应alipayUserId，标准userId格式，2088开头的16位纯数字

                //使用auth_token获取用户基本信息
                $request = new AlipayUserUserinfoShareRequest();
                $request->getApiMethodName();
                //$request->setTerminalInfo($auth_token);

                //var_dump($auth_token);
                $result = aopclient_request_execute($request, $auth_token);

                echo "<pre>";
                print_r($result);

            }
        }


        //事件消息
        //https://fuwu.alipay.com/platform/doc.htm#c0302
        if(HttpRequest::getRequest('param') == 'msgnotify ')
        {
            $biz_content = '{"id" : "100711"}';

            $request = new AlipayMobilePublicContactFollowListRequest();
            $request->setBizContent($biz_content);

            $result = aopclient_request_execute($request);

            echo "<pre>";
            print_r($result);
            if($result != null && $result->alipay_mobile_public_label_delete_response->code == 200)
            {
                echo $result->alipay_mobile_public_label_delete_response->id;

            }
        }


//        var_dump($auth_token);

        $this->smarty->assign('auth_token', $auth_token);

        $this->smarty->assign('auth_code', $auth_code);
        $this->smarty->display("alipay.tpl");


    }

    public function getauthcode()
    {
        echo "<pre>";
        var_dump($_GET);
    }



}
?>